﻿using System;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.WebControls;


namespace Voodoo.SharePoint.ReadUnreadMarks
{
    //This is the control that renders on SharePoint column configuration
    //the host control is ReadUnreadDataFieldPropertyEditor.ascx
    //page. Nothing fancy here, just plain old C# codebehind
    public partial class ReadUnreadFieldPropertyEditor : UserControl, Microsoft.SharePoint.WebControls.IFieldEditor
    {
        #region Declarations
        private string _ReadImageUrl = string.Empty;
        private string _UnreadImageUrl = string.Empty;
        private string _UnreadHTMLColor = "RED";

        private bool _ClearReadMarksOnUpdate = false;
        private bool _ShowInlineEditingTools = false;
        private bool _VisibleOnForm = false;
        private bool _Looping = false;
        private string _ColumnRenderMode = string.Empty;

        #endregion

        #region UserControl imlementation
        public override void Focus()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (!Page.IsPostBack)
            {
                txtUnreadImageUrl.Enabled = false;
                txtReadImageUrl.Enabled = false;
                radColumnDisplayIconic.Checked = false;
                radColumnDisplayBold.Checked = false;
                radColumnClearReadMarks.Checked = _ClearReadMarksOnUpdate;
                radColumnRetainReadMarks.Checked = !_ClearReadMarksOnUpdate;
                chkShowInlineEditingTools.Checked = _ShowInlineEditingTools;
                chkInvisibleOnForm.Checked = !_VisibleOnForm;
                txtUnreadImageUrl.Text = _UnreadImageUrl;
                txtReadImageUrl.Text = _ReadImageUrl;
                txtUnreadHTMLColor.Text = _UnreadHTMLColor;
                switch (_ColumnRenderMode)
                {
                    case ReadUnreadField.ICON_DISPLAY:
                        radColumnDisplayIconic.Checked = true;
                        break;
                    case ReadUnreadField.BOLD_DISPLAY:
                    default:
                        radColumnDisplayBold.Checked = true;
                        break;
                }
                if (!UtilityMethods.CurrentConfigurationSupportsListType(SPContext.Current.List))
                {
                    //find control does not work for the description so we 
                    //get the parent and loop throught them 
                    PlaceHolder placeHolderDescription = (PlaceHolder)ReadUnreadPropertySection.TemplateControl.FindControl("PlaceHolderDescription");
                    if (null != placeHolderDescription)
                    {
                        foreach (Control tempControl in placeHolderDescription.Controls)
                        {
                            System.Web.UI.LiteralControl literalDescription = tempControl as System.Web.UI.LiteralControl;
                            if (null != literalDescription)
                            {
                                literalDescription.Text += "<B/><B/><div class='ms-error'>Warning document library support has not been enabled ! ";
                                literalDescription.Text += " You may add and configure this colum however it will not function";
                                literalDescription.Text += " correctly until the SharePoint administrator enables <BOLD>Document read tracking</BOLD>.</div>";
                                break;
                            }
                        }
                    }

                }
            }

            DisplayMode_CheckedChanged();
            radColumnDisplayBold.CheckedChanged += new EventHandler(ColumnDisplayBold_CheckedChanged);
            radColumnDisplayIconic.CheckedChanged += new EventHandler(ColumnDisplayIconic_CheckedChanged);
            radColumnRetainReadMarks.CheckedChanged += new EventHandler(ColumnRetainReadMarks_CheckedChanged);
            radColumnClearReadMarks.CheckedChanged += new EventHandler(ColumnClearReadMarks_CheckedChanged);
        }

        #endregion

        #region Radio button stuff
        //The radio buttion group names do not hold accross the HTML areas
        //created by the SharePoint input form areas, so we will wire them
        //up to event handlers and do the UI work internally
        void ColumnDisplayIconic_CheckedChanged(object sender, EventArgs e)
        {
            if (!_Looping)
            {
                _Looping = true;
                radColumnDisplayBold.Checked = !radColumnDisplayIconic.Checked;
                DisplayMode_CheckedChanged();
                _Looping = false;
            }
        }

        void ColumnDisplayBold_CheckedChanged(object sender, EventArgs e)
        {
            if (!_Looping)
            {
                _Looping = true;
                radColumnDisplayIconic.Checked = !radColumnDisplayBold.Checked;
                DisplayMode_CheckedChanged();
                _Looping = false;
            }
        }
    

        void ColumnClearReadMarks_CheckedChanged(object sender, EventArgs e)
        {
            if (!_Looping)
            {
                _Looping = true;
                //everything else posts back so setup this one to do the same
                _ClearReadMarksOnUpdate = radColumnClearReadMarks.Checked;
                radColumnRetainReadMarks.Checked = !_ClearReadMarksOnUpdate;
                _Looping = false;
            }
        }

        void ColumnRetainReadMarks_CheckedChanged(object sender, EventArgs e)
        {
            if (!_Looping)
            {
                _Looping = true;
                //everything else posts back so setup this one to do the same
                _ClearReadMarksOnUpdate = !radColumnRetainReadMarks.Checked;
                radColumnClearReadMarks.Checked = _ClearReadMarksOnUpdate;
                _Looping = false;
            }
        }
        void DisplayMode_CheckedChanged()
        {
       
            if (radColumnDisplayIconic.Checked)
            {
                txtUnreadImageUrl.Enabled = true;
                txtReadImageUrl.Enabled = true;
                txtUnreadHTMLColor.Text = "RED";
                txtUnreadHTMLColor.Enabled = false;
            }
            else
            {
                txtUnreadImageUrl.Text = "/_layouts/images/Voodoo.ListItemReadUnreadSiteColumn/clscUnreadMark.gif";
                txtReadImageUrl.Text = "/_layouts/images/Voodoo.ListItemReadUnreadSiteColumn/clscReadMark.gif";
                txtUnreadImageUrl.Enabled = false;
                txtReadImageUrl.Enabled = false;
                txtUnreadHTMLColor.Enabled = true;
            }
        }

        #endregion

        #region Utilities


        private string ColumnRenderMode()
        {
            if (radColumnDisplayIconic.Checked) return ReadUnreadField.ICON_DISPLAY;
            return ReadUnreadField.BOLD_DISPLAY;
        }
        #endregion

        #region IFieldEditor implementation
        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            if (null != field)
            {
                ReadUnreadField sharePointField = (ReadUnreadField)field;
                _ClearReadMarksOnUpdate = sharePointField.ClearReadMarksOnUpdate;
                _ColumnRenderMode = sharePointField.ColumnRenderMode;
                _ShowInlineEditingTools = sharePointField.ShowInlineEditingTools;
                _VisibleOnForm = sharePointField.VisibleOnForm;
                _ReadImageUrl = sharePointField.ReadImageUrl;
                _UnreadImageUrl = sharePointField.UnreadImageUrl;
                _UnreadHTMLColor = sharePointField.UnreadHTMLColor;
            }
        }


        public void OnSaveChange(SPField field, bool isNewField)
        {
            ReadUnreadField sharePointField = (ReadUnreadField)field;
            sharePointField.ColumnRenderMode = ColumnRenderMode();
            sharePointField.ClearReadMarksOnUpdate = radColumnClearReadMarks.Checked;
            sharePointField.ShowInlineEditingTools = chkShowInlineEditingTools.Checked;
            sharePointField.VisibleOnForm = !chkInvisibleOnForm.Checked;
            sharePointField.UnreadImageUrl = txtUnreadImageUrl.Text;
            sharePointField.ReadImageUrl = txtReadImageUrl.Text;
            sharePointField.UnreadHTMLColor = txtUnreadHTMLColor.Text;
            if (isNewField)
            {
                //if the field is new, we will set the global new property cache
                //which helps us avoid all of the conflicts between update states
                //that can occur during postback processing of the control
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.COLUMN_RENDER_MODE_KEY, ColumnRenderMode());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.CLEAR_READ_MARKS_ON_UPDATE_KEY, radColumnClearReadMarks.Checked.ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.SHOW_INLINE_EDITING_TOOLS_KEY, chkShowInlineEditingTools.Checked.ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.VISIBLE_ON_FORM_KEY, (!chkInvisibleOnForm.Checked).ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.UNREAD_IMAGE_URL_KEY, txtUnreadImageUrl.Text);
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.READ_IMAGE_URL_KEY, txtReadImageUrl.Text);
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadField.UNREAD_HTML_COLOR_KEY, txtUnreadHTMLColor.Text);
            }
        }
        #endregion
    }
}
